﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using popLib.CustomeControl;
using System.Data.Objects;
using popLib;

namespace popLib.ModelSystem
{
    public class FormManagement : Form
    {
        public dynamic Entity { get; set; }
        public dynamic SYS_TaiKhoan { get; set; }

        public List<OptionPanelItem> Panels { get; set; }
        
        OptionPanel panelControl;

        public FormManagement()
        {
            
            Panels = new List<OptionPanelItem>();
            Entity = MainFormModel.Instance.GetDBObjectContextFunction();
            SYS_TaiKhoan = Entity.SYS_TaiKhoan;

            Panels.Add(genEFDataOptionPanel("Role", Entity, Entity.SYS_Role, "Role_ID", new List<SearchItem>(){
                new SearchItem(DataLib.GenColumn("Role_ID", "Role_ID").SetAlign(DataGridViewContentAlignment.MiddleCenter), false),
                new SearchItem(DataLib.GenColumn("Tên role", "TenRole").SetSizeMode( DataGridViewAutoSizeColumnMode.Fill).SetReadOnly(false)),
            }));

            Panels.Add(genEFDataOptionPanel("Account - Role", Entity, Entity.SYS_TaiKhoan_Role, "TaiKhoan_Role_ID", new List<SearchItem>(){
                new SearchItem(DataLib.GenColumn("MID", "TaiKhoan_Role_ID").SetAlign(DataGridViewContentAlignment.MiddleCenter), false),
                new SearchItem(DataLib.GenColumn("Tên Role", "Role_ID", DataLib.GenColumnType.ComboBox).SetComboBoxDataSource((object)Entity.SYS_Role, "TenRole", "Role_ID").SetReadOnly(false).SetSizeMode(DataGridViewAutoSizeColumnMode.None).SetWidth(200) ),
                new SearchItem(DataLib.GenColumn("Tài khoản", "TaiKhoan", DataLib.GenColumnType.ComboBox).SetComboBoxDataSource((object)SYS_TaiKhoan, "TenDangNhap", "TenDangNhap").SetReadOnly(false).SetSizeMode(DataGridViewAutoSizeColumnMode.None).SetWidth(100)).SetSearchText(),
                new SearchItem(DataLib.GenColumn("Tên nhân viên", "TaiKhoan", DataLib.GenColumnType.ComboBox).SetComboBoxDataSource((object)SYS_TaiKhoan, "TenNhanVien", "TenDangNhap").SetReadOnly(false).SetSizeMode(DataGridViewAutoSizeColumnMode.None).SetWidth(200)).SetSearchText("SYS_TaiKhoan.TenNhanVien"),
                new SearchItem(DataLib.GenColumn("Sử dụng", "SuDung", DataLib.GenColumnType.CheckBox).SetReadOnly(false)),
            }));

            Panels.Add(genEFDataOptionPanel("Module", Entity, Entity.SYS_Module, "Module_ID", new List<SearchItem>(){
                new SearchItem(DataLib.GenColumn("Module_ID", "Module_ID").SetAlign(DataGridViewContentAlignment.MiddleCenter), false),
                new SearchItem(DataLib.GenColumn("Tên Module", "TenModule").SetSizeMode( DataGridViewAutoSizeColumnMode.Fill).SetReadOnly(false) ),
            }));

            Panels.Add(genEFDataOptionPanel("Form", Entity, Entity.SYS_Form, "Form_ID", new List<SearchItem>(){
                new SearchItem(DataLib.GenColumn("Form_ID", "Form_ID").SetAlign(DataGridViewContentAlignment.MiddleCenter), false),
                new SearchItem(DataLib.GenColumn("Tên form", "TenForm").SetReadOnly(false)),
                new SearchItem(DataLib.GenColumn("Module", "Module_ID", DataLib.GenColumnType.ComboBox).SetReadOnly(false).SetComboBoxDataSource((object)Entity.SYS_Module,"TenModule","Module_ID")),
                new SearchItem(DataLib.GenColumn("Path", "PathForm").SetSizeMode(DataGridViewAutoSizeColumnMode.Fill).SetReadOnly(false)),
            }));

            Panels.Add(genEFDataOptionPanel("Menu", Entity, Entity.SYS_Menu, "Menu_ID", new List<SearchItem>(){
                new SearchItem(DataLib.GenColumn("Menu_ID", "Menu_ID").SetAlign(DataGridViewContentAlignment.MiddleCenter), false),
                new SearchItem(DataLib.GenColumn("Tên Menu", "TenMenu").SetSizeMode(DataGridViewAutoSizeColumnMode.Fill).SetReadOnly(false)),
                new SearchItem(DataLib.GenColumn("Parent menu ID", "ParentMenu_ID").SetReadOnly(false)),
                new SearchItem(DataLib.GenColumn("Tên form", "Form_ID", DataLib.GenColumnType.ComboBox).SetComboBoxDataSource((object)Entity.SYS_Form, "TenForm", "Form_ID").SetReadOnly(false)),
                new SearchItem(DataLib.GenColumn("MenuIndex", "MenuIndex").SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Top Separator", "TopSeparator", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
            }));

            Panels.Add(genEFDataOptionPanel("Role - Form", Entity, Entity.SYS_Role_Form, "Role_Form_ID", new List<SearchItem>(){
                new SearchItem(DataLib.GenColumn("Role", "Role_ID", DataLib.GenColumnType.ComboBox).SetReadOnly(false).SetComboBoxDataSource((object)Entity.SYS_Role, "TenRole", "Role_ID").SetSizeMode(DataGridViewAutoSizeColumnMode.None).SetWidth(150)),
                new SearchItem(DataLib.GenColumn("Form", "Form_ID", DataLib.GenColumnType.ComboBox).SetReadOnly(false).SetComboBoxDataSource((object)Entity.SYS_Form,"TenForm","Form_ID").SetSizeMode(DataGridViewAutoSizeColumnMode.None).SetWidth(200)).SetSearchText("SYS_Form.TenForm"),
                new SearchItem(DataLib.GenColumn("Full Permission", "FullPermission", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),                
                new SearchItem(DataLib.GenColumn("Add Record", "AddRecord", DataLib.GenColumnType.CheckBox).SetReadOnly(false).SetSortMode(DataGridViewColumnSortMode.NotSortable), false),
                new SearchItem(DataLib.GenColumn("View Record", "ViewRecord", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("View Record Owner", "ViewRecordOwner", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Edit Record", "EditRecord", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Edit Record Owner", "EditRecordOwner", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Dispose Record", "DisposeRecord", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Dispose Record Owner", "DisposeRecordOwner", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Confirm Record", "ConfirmRecord", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Confirm Record Owner", "ConfirmRecordOwner", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Print Record", "PrintRecord", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Print Record Owner", "PrintRecordOwner", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),                
                new SearchItem(DataLib.GenColumn("Sử dụng", "SuDung", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Extra Parameter", "ExtraParam").SetReadOnly(false), false),
            }));

            Panels.Add(genEFDataOptionPanel("Đặc quyền", Entity, Entity.SYS_DacQuyen, "DacQuyen_ID", new List<SearchItem>(){
                new SearchItem(DataLib.GenColumn("ID", "DacQuyen_ID").SetAlign(DataGridViewContentAlignment.MiddleCenter), false),
                new SearchItem(DataLib.GenColumn("Tài khoản", "TaiKhoan", DataLib.GenColumnType.ComboBox).SetComboBoxDataSource((object)SYS_TaiKhoan, "TenDangNhap", "TenDangNhap").SetReadOnly(false).SetSizeMode(DataGridViewAutoSizeColumnMode.None).SetWidth(100)).SetSearchText(),
                new SearchItem(DataLib.GenColumn("Tên nhân viên", "TaiKhoan", DataLib.GenColumnType.ComboBox).SetComboBoxDataSource((object)SYS_TaiKhoan, "TenNhanVien", "TenDangNhap").SetReadOnly(false).SetSizeMode(DataGridViewAutoSizeColumnMode.None).SetWidth(200)).SetSearchText("DM_TaiKhoanNguoiDung.DM_NhanVien.TenNhanVien"),
                new SearchItem(DataLib.GenColumn("Form", "Form_ID", DataLib.GenColumnType.ComboBox).SetReadOnly(false).SetComboBoxDataSource((object)Entity.SYS_Form,"TenForm","Form_ID").SetSizeMode(DataGridViewAutoSizeColumnMode.None).SetWidth(200)).SetSearchText("SYS_Form.TenForm"),
                new SearchItem(DataLib.GenColumn("Full Permission", "FullPermission", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),                
                new SearchItem(DataLib.GenColumn("Add Record", "AddRecord", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("View Record", "ViewRecord", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("View Record Owner", "ViewRecordOwner", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Edit Record", "EditRecord", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Edit Record Owner", "EditRecordOwner", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Dispose Record", "DisposeRecord", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Dispose Record Owner", "DisposeRecordOwner", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Confirm Record", "ConfirmRecord", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Confirm Record Owner", "ConfirmRecordOwner", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Print Record", "PrintRecord", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Print Record Owner", "PrintRecordOwner", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Sử dụng", "SuDung", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
                new SearchItem(DataLib.GenColumn("Extra Parameter", "ExtraParam").SetReadOnly(false), false),
            }));

            Panels.Add(genEFDataOptionPanel("Tài khoản người dùng", Entity, Entity.SYS_TaiKhoan, "NgayCapNhat", new List<SearchItem>(){
                new SearchItem(DataLib.GenColumn("Tên đăng nhập", "TenDangNhap").SetReadOnly(false)),
                new SearchItem(DataLib.GenColumn("Mật khẩu", "MatKhau").SetReadOnly(false)),
                new SearchItem(DataLib.GenColumn("Tên nhân viên", "TenNhanVien").SetReadOnly(false)),
                new SearchItem(DataLib.GenColumn("Người cập nhật", "NguoiCapNhat").SetReadOnly(false)),
                new SearchItem(DataLib.GenColumn("Ngày cập nhật", "NgayCapNhat", DataLib.GenColumnType.DateTime).SetFormat("dd/MM/yyyy").SetReadOnly(false)),
                new SearchItem(DataLib.GenColumn("Sử dụng", "SuDung", DataLib.GenColumnType.CheckBox).SetReadOnly(false), false),
            }));

            panelControl = new OptionPanel();
            panelControl.Panel = Panels.ToArray();
            panelControl.Dock = DockStyle.Fill;
            this.Controls.Add(panelControl);
            this.Size = new System.Drawing.Size(1000, 671);
            this.Icon = MainFormModel.Instance.AppIconSmall;
        }

        OptionPanelItem genEFDataOptionPanel(string title, ObjectContext entity, dynamic dataset, string orderByField, List<SearchItem> searchList)
        {
            OptionPanelItem container = new OptionPanelItem();
            container.PanelTitle = title;

            EFDataMgrPanel control = new EFDataMgrPanel();
            control.InitForm(entity, dataset, searchList, MainFormModel.Instance.ItemPerPage <= 0 ? 100 : MainFormModel.Instance.ItemPerPage, orderByField);
            control.Dock = DockStyle.Fill;
            control.UpdateModeChanged += new EFDataMgrPanel.UpdateModeChangedEvent(control_UpdateModeChanged);
            control.DataSaved += new EFDataMgrPanel.DataSavedEvent(control_DataSaved);

            container.Controls.Add(control);

            return container;
        }

        void control_DataSaved(EFDataMgrPanel control)
        {
            Entity = MainFormModel.Instance.GetDBObjectContextFunction();

            if (control.ObjectDataSet.GetType().FullName.ToUpper().Contains("SYS_ROLE"))
            {
                ((EFDataMgrPanel)Panels[1].Controls[0]).SearchList[1].ReloadCoboBoxDataSource(Entity.SYS_Role, "TenRole", "Role_ID");
                ((EFDataMgrPanel)Panels[5].Controls[0]).SearchList[0].ReloadCoboBoxDataSource(Entity.SYS_Role, "TenRole", "Role_ID");
            }
            else if (control.ObjectDataSet.GetType().FullName.ToUpper().Contains("SYS_MODULE"))
            {
                ((EFDataMgrPanel)Panels[3].Controls[0]).SearchList[2].ReloadCoboBoxDataSource(Entity.SYS_Module, "TenModule", "Module_ID");
            }
            else if (control.ObjectDataSet.GetType().FullName.ToUpper().Contains("SYS_FORM"))
            {
                ((EFDataMgrPanel)Panels[4].Controls[0]).SearchList[3].ReloadCoboBoxDataSource(Entity.SYS_Form, "TenForm", "Form_ID");
                ((EFDataMgrPanel)Panels[5].Controls[0]).SearchList[1].ReloadCoboBoxDataSource(Entity.SYS_Form, "TenForm", "Form_ID");
                ((EFDataMgrPanel)Panels[6].Controls[0]).SearchList[3].ReloadCoboBoxDataSource(Entity.SYS_Form, "TenForm", "Form_ID");
            }
        }

        void control_UpdateModeChanged(EFDataMgrPanel control, bool updateMode)
        {
            panelControl.ButtonTabReadOnly = updateMode;
        }
    }
}
